import time

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.utils import web_driver
from config import URL1


class OrderPlacementBusiness:

    def user_login(self,driver,user,pwd):
        driver.find_element(by=By.XPATH, value="/html/body/div/div[2]/div[3]/div[2]").click()
        driver.find_element(by=By.XPATH, value="/html/body/div/div/div[2]/div[1]/div[1]/input").send_keys(user)
        driver.find_element(by=By.XPATH, value="/html/body/div/div/div[2]/div[2]/div[1]/input").send_keys(pwd)
        driver.find_element(by=By.CSS_SELECTOR, value=".van-button.van-button--danger.van-button--large").click()

    def search_products(self,driver,content):
        driver.find_element(by=By.XPATH, value="/html/body/div/div[2]/div[1]/div[2]").click()
        driver.find_element(by=By.CSS_SELECTOR, value=".van-field__control").click()
        time.sleep(1)
        driver.find_element(by=By.CSS_SELECTOR, value=".van-field__control").send_keys(content)
        # time.sleep(1)
        # 点击键盘的enter键,才能搜索到信息
        driver.find_element(by=By.CSS_SELECTOR, value=".van-field__control").send_keys(Keys.ENTER)


    def add_cart(self,driver,num):
        driver.find_elements(by=By.CSS_SELECTOR, value=".van-card__content")[0].click()
        driver.find_element(by=By.CSS_SELECTOR, value=".van-button.van-button--warning.van-button--large.van-button--square.van-goods-action-button.van-goods-action-button--first.van-goods-action-button--warning").click()
        time.sleep(1)
        driver.find_element(by=By.CSS_SELECTOR, value=".van-stepper__input").click()
        # control+a : 全选
        driver.find_element(by=By.CSS_SELECTOR, value=".van-stepper__input").send_keys((Keys.CONTROL + 'A'))
        # 删除
        driver.find_element(by=By.CSS_SELECTOR, value=".van-stepper__input").send_keys(Keys.DELETE)
        driver.find_element(by=By.CSS_SELECTOR, value=".van-stepper__input").send_keys(num)
        driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[3]/div[3]/div[3]/button[1]").click()

    def submit_order(self, driver):
        driver.find_element(by=By.CSS_SELECTOR, value=".van-icon.van-icon-cart-o.van-goods-action-icon__icon").click()
        driver.find_element(by=By.CSS_SELECTOR, value=".van-button.van-button--danger.van-button--normal.van-button--round.van-submit-bar__button.van-submit-bar__button--danger").click()
        time.sleep(2)
        # 显示等待
        # WebDriverWait(driver,10).until(lambda x:x.find_element(by=By.CSS_SELECTOR, value=".van-button.van-button--danger.van-button--normal.van-button--round.van-submit-bar__button.van-submit-bar__button--danger")).click()
        driver.find_element(by=By.CSS_SELECTOR, value=".van-button.van-button--danger.van-button--normal.van-button--round.van-submit-bar__button.van-submit-bar__button--danger").click()
        time.sleep(2)
        # WebDriverWait(driver,10).until(lambda x:x.find_element(by=By.CSS_SELECTOR, value=".van-button.van-button--danger.van-button--normal.van-button--round.van-submit-bar__button.van-submit-bar__button--danger")).click()
        # driver.find_element(by=By.CSS_SELECTOR, value=".pay_submit.van-button.van-button--primary.van-button--normal").click()
        # time.sleep(2)
        WebDriverWait(driver,10).until(lambda x:x.find_element(by=By.CSS_SELECTOR, value=".pay_submit.van-button.van-button--primary.van-button--normal")).click()
        driver.find_element(by=By.CSS_SELECTOR, value=".van-button.van-button--default.van-button--large.van-dialog__confirm").click()


if __name__ == '__main__':
    opb = OrderPlacementBusiness()
    driver = web_driver()
    driver.get(URL1)
    opb.user_login(driver, "abc", "123456")
    opb.search_products(driver, "测试")
    opb.add_cart(driver, 5)
    opb.submit_order(driver)
    time.sleep(5)



